﻿using Abp.Domain.Repositories;
using Abp.Domain.Services;
using Abp.UI;
using shenghua.Common.WordNodes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shenghua.Member.ShippingAddresses
{
    public class MemberShippingAddressManager: DomainService
    {
        IRepository<MemberShippingAddress> _memberShippingAddressRepository;

        public MemberShippingAddressManager(IRepository<MemberShippingAddress> memberShippingAddressRepository)
        {
            _memberShippingAddressRepository = memberShippingAddressRepository;
        }

        public async Task<MemberShippingAddress> Get(long memberId, int id)
        {
            var memberShippingAddress = _memberShippingAddressRepository.GetAll().Where(r => r.MemberId == memberId && r.Id == id).FirstOrDefault();
            if(memberShippingAddress == null)
            {
                throw new UserFriendlyException("不存在的会员收货地址");
            }
            return memberShippingAddress;
        }

        public async Task<MemberShippingAddress> Add(long memberId, MemberShippingAddress memberShippingAddress)
        {
            memberShippingAddress.MemberId = memberId;
            await _memberShippingAddressRepository.InsertAsync(memberShippingAddress);
            if(memberShippingAddress.IsDefault)
            {
                await SetDefault(memberId, memberShippingAddress.Id);
            }
            return memberShippingAddress;
        }

        public async Task<MemberShippingAddress> Update(long memberId, MemberShippingAddress memberShippingAddress)
        {
            if(memberShippingAddress.MemberId != memberId)
            {
                throw new UserFriendlyException("当前地址不属于该用户");
            }
            await _memberShippingAddressRepository.UpdateAsync(memberShippingAddress);
            if (memberShippingAddress.IsDefault)
            {
                await SetDefault(memberId, memberShippingAddress.Id);
            }
            return memberShippingAddress;
        }

        public async Task Delete(long memberId, int id)
        {
            await _memberShippingAddressRepository.DeleteAsync(r => r.MemberId == memberId && r.Id == id);
        }

        public async Task<List<MemberShippingAddress>> GetAll(long memberId)
        {
            return await _memberShippingAddressRepository.GetAllListAsync(r => r.MemberId == memberId);
        }

        public async Task SetDefault(long memberId, int id)
        {
            MemberShippingAddress current = await Get(memberId, id);
            if (!current.IsDefault)
            {
                current.IsDefault = true;
                _memberShippingAddressRepository.Update(current);
            }

            var olds = _memberShippingAddressRepository.GetAllList(r => r.MemberId == memberId && r.Id != id && r.IsDefault == true);
            foreach(var item in olds)
            {
                item.IsDefault = false;
                _memberShippingAddressRepository.Update(item);
            }
        }

        public async Task<MemberShippingAddress> GetDefault(long memberId)
        {
            var defaultAddress = _memberShippingAddressRepository.GetAll().Where(r => r.MemberId == memberId && r.IsDefault).FirstOrDefault();
            if(defaultAddress == null)
            {
                defaultAddress = _memberShippingAddressRepository.GetAll().Where(r => r.MemberId == memberId).FirstOrDefault();
            }
            return defaultAddress;
        }
    }
}
